package com.example.projectdemo.materialComponents;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

import com.example.projectdemo.R;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.button.MaterialButtonToggleGroup;

import java.util.List;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

/***
 * 遗留问题:关于反射的问题,拿不到inset,MaterialButtonHelper中的属性
 */
public class MaterialButtonActivity extends AppCompatActivity {

    private MaterialButtonToggleGroup toggleGroup;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_button);

        toggleGroup = findViewById(R.id.toggleGroup);
        MaterialButtonToggleGroup toggleGroupVertical = findViewById(R.id.toggleGroupVertical);
        //动态添加 按钮
        //toggleGroup.addView（button3,2,layoutParams）
        MaterialButton button = new MaterialButton(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        layoutParams.setMargins(0, 0, 0, 0);
        button.setText("1234");
        toggleGroupVertical.addView(button, 3, layoutParams);
        //
        int checkedButtonId = toggleGroup.getCheckedButtonId();
        List<Integer> checkedButtonIds = toggleGroup.getCheckedButtonIds();
        Log.i("zxd", "onCreate: " + checkedButtonId);

        toggleGroup.clearChecked(); // Unchecks all buttons
        toggleGroup.uncheck(R.id.private_btn); // Unchecks a specific button
        toggleGroup.check(R.id.private_btn); // Checks a specific button

        toggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
            @Override
            public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
                Log.i("zxd", "onButtonChecked: " + checkedId);
                Log.i("zxd", "onButtonChecked: " + isChecked);
                //切换按钮的日志
                //2021-06-19 15:29:53.297 13531-13531/? I/zxd: onButtonChecked: 2131231021
                //2021-06-19 15:29:53.297 13531-13531/? I/zxd: onButtonChecked: false
                //2021-06-19 15:29:53.298 13531-13531/? I/zxd: onButtonChecked: 1
                //2021-06-19 15:29:53.298 13531-13531/? I/zxd: onButtonChecked: true
            }
        });

        toggleGroup.isSelectionRequired();
        toggleGroup.isSingleSelection();

        toggleGroup.setSelectionRequired(false);
        toggleGroup.setSingleSelection(true);


        /*// Using icon resource ID
        textButton.setIconResource(R.drawable.ic_show_black_18dp)
        // Using icon Drawable
        val showDrawable = AppCompatResources.getDrawable(context, R.drawable.ic_show_black_18dp)
        textButton.icon = showDrawable*/
    }
}